package com.ruimei.service.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.ruimei.controller.LoginController;
import com.ruimei.entitys.Code;
import com.ruimei.entitys.Parent;
import com.ruimei.entitys.Teacher;
import com.ruimei.mapper.PassWordMapper;
import com.ruimei.service.PassWordService;
import com.ruimei.util.JSONUtils;
import com.ruimei.util.Md5Encrypt;
import com.ruimei.util.SmsUtils;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class PassWordServiceImpl
  implements PassWordService
{

  @Resource
  private  RedisTemplate<String,String> redisTemplate;
  @Resource
  PassWordMapper passWordMapper;
  
  private static Boolean time(Date updateDate)
  {
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    Calendar c3 = Calendar.getInstance();
    c1.setTime(updateDate);
    c2.setTime(new Date());
    c3.setTime(new Date());
    c3.add(12, 5);
    c2.add(12, -5);
    if ((c1.after(c2)) && (c1.before(c3)))
    {
      //System.out.println("五分钟之内");
      return Boolean.valueOf(true);
    }
    //System.out.println("五分钟之外");
    return Boolean.valueOf(false);
  }

  @Override
  public Map<String, Object> setPasswordByteacherId(String teacherCode, String newPwd, Teacher teacher)
  {
    List<Code> list = this.passWordMapper.selectCodeByPhoneNo(teacher.getPhoneNo());
    Map<String, Object> map = checkCode(teacher, list, teacherCode, newPwd);
    
    return map;
  }
  
  public <T> Map<String, Object> checkCode(T entity, List<Code> list, String userCode, String newPwd)
  {
    Map<String, Object> map = new HashMap();
    String phoneNo = null;
    if (list.size() > 0)
    {
      if (time(((Code)list.get(0)).getVtime()).booleanValue())
      {
        if (((Code)list.get(0)).getVcode().equals(userCode))
        {
          Integer i = null;
          if ((entity instanceof Teacher))
          {
            Teacher t = (Teacher)entity;
            phoneNo = t.getPhoneNo();
            String secretkey = Md5Encrypt.MdPwd(newPwd, t.getSalt());
            i = this.passWordMapper.setPasswordByteacherId(t.getId(), secretkey);
          }
          else if ((entity instanceof Parent))
          {
            Parent parent = (Parent)entity;
            phoneNo = parent.getPhoneNo();
            String secretkey = Md5Encrypt.MdPwd(newPwd, parent.getSalt());
            i = this.passWordMapper.setPasswordByParentId(parent.getId(), secretkey);
          }
          else
          {
            map.put("status", Boolean.valueOf(false));
            map.put("message", "用户错误");
          }
          if (i.intValue() == 1)
          {
            map.put("status", Boolean.valueOf(true));
            map.put("message", "修改成功");
            this.passWordMapper.clearCodeByPhoneNo(phoneNo, null);
          }
          else
          {
            map.put("status", Boolean.valueOf(false));
            map.put("message", "修改未成功");
          }
        }
        else
        {
          map.put("status", Boolean.valueOf(false));
          map.put("message", "验证码错误");
        }
      }
      else
      {
        map.put("status", Boolean.valueOf(false));
        map.put("message", "验证码超时");
      }
    }
    else
    {
      map.put("status", Boolean.valueOf(false));
      map.put("message", "请先获取验证码");
    }
    return map;
  }

  @Override
  public Map<String, Object> getCode(String phoneNo)
    throws ClientException
  {
    Map<String, Object> map = new HashMap();
    SmsUtils.setNewcode();
    String code = Integer.toString(SmsUtils.getNewcode());
    SendSmsResponse sendSms = SmsUtils.sendSms(phoneNo, code);
    if ((sendSms.getMessage().equals("OK")) && (sendSms.getCode().equals("OK")))
    {
      map.put("status", Boolean.valueOf(true));
      map.put("message", "短信发送成功");
      this.passWordMapper.clearCodeByPhoneNo(phoneNo, null);
      this.passWordMapper.addCodeByPhoneNo(phoneNo, code);
    }
    else
    {
      map.put("status", Boolean.valueOf(false));
      map.put("message", "短信发送失败");
    }
    return map;
  }

  @Override
  public Map<String, Object> getTeacherCode(Teacher teacher)
    throws ClientException
  {
    Map<String, Object> map = getCode(teacher.getPhoneNo());
    
    return map;
  }

  @Override
  public Map<String, Object> setPasswordByParentId(Parent parent,String parentCode, String newPwd)
  {
    List<Code> list = this.passWordMapper.selectCodeByPhoneNo(parent.getPhoneNo());
    Map<String, Object> map = checkCode(parent, list, parentCode, newPwd);
    
    return map;
  }

  @Override
  public Map<String, Object> getParentCode(String parentId)
    throws ClientException
  {
    Map<String,Object> map = new HashMap<>();

    if(redisTemplate.hasKey(parentId)){
      Parent parent = (Parent) JSONUtils.jsonStringToBean(redisTemplate.opsForValue().get(parentId),Parent.class);
      map    =   getCode(parent.getPhoneNo());
    }
    return map;
  }

  @Override
  public Teacher getTeacherByphoneNo(String phoneNo) {
    Teacher teacher = passWordMapper.getTeacherByphoneNo(phoneNo);
    return teacher;
  }

  @Override
  public Parent getParentByphoneNo(String phoneNo) {
    Parent parent = passWordMapper.getParentByphoneNo(phoneNo);
    return parent;
  }
}


/* Location:              E:\360MoveData\Users\admin\Desktop\teacher_wap-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes\com\ruimei\service\impl\PassWordServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */